KEYPAD INTERFACING WITH 8051
A miniature keyboard or set of buttons for operating a portable electronic device, telephone, or other equipment. This topic is about interfacing of 4X3 matrix keypad with AT89S52 microcontroller.
Synopsis

The keypad or keyboard is an input device and we all are  very familiar with it. Keyboard is used in our ATM Machine, Computer, Mobile Phone etc. but the number  of keys varies. LCD is used as a display the corresponding key values.

Description

Keypad is organized as a matrix of switches in rows and column. Here uses a 4X3 matrix keypad and a 16x2 LCD for displaying the output of keypad. The concept of interfacing keypad with the MCU is simple. Every number is assigned two unique parameters, i.e., row and column number (n(R, C) for example 6 (2, 3)). Hence every time a key is pressed the number is identified by detecting the row and column number of the key pressed.

Initially all the rows are set to zero & columns are set to one by the controller and the columns are scanned to check if any key is pressed. In case no key is pressed the output of all the columns will be high. Whenever a key is pressed the row and column corresponding to the key will get short, resulting in the output of the corresponding column goes to go low (since we have made all the rows zero). This gives the column number of the pressed key.

Once the column number is detected, the controller set’s all the rows to high. Now one by one each row is set to zero by controller and the earlier detected column is checked if it becomes zero. The row corresponding to which the column gets zero is the row number of the digit. The above process is very fast and even if the switch is pressed for a very small duration of time the controller can detect the key which is pressed. The controller displays the number corresponding to the row and column on the LCD.


Proteus design for Keypad interfacing with 8051


Orcad design for Keypad interfacing with 8051


Keypad interfacing with 8051

/*  Name     : main.c
 *  Purpose  : Source code for KEYPAD Interfacing with AT89C52.
 *  Author   : Gemicates
 *  Date     : 2014-01-19
 *  Website  : www.gemicates.org
 *  Revision : None
 */
 
#include <REGX52.H>											// header file for AT89c52 series
#define keypad P0
#define Lcd_port P2											// GPIO direction register declaration

sbit rs = P3^7;  											// register select pin
sbit rw = P3^6;  											// read write pin
sbit en = P3^5;  											// enable pin

 													// Row and Column pins declaration
sbit R1 = keypad^0;
sbit R2 = keypad^1;
sbit R3 = keypad^2;
sbit R4 = keypad^3;
sbit C1 = keypad^4;
sbit C2 = keypad^5;
sbit C3 = keypad^6;

 				                                                                        // Function declarations
void delay(unsigned int count);
void lcdcmd_address(unsigned char cmd);
void lcddata(unsigned char send_data);
void lcd_string(unsigned char str[10]);
void lcd_data_string(unsigned char *str);

void check_column_one();
void check_column_two();
void check_column_three();
void output(int key);

void main()											       // main function
{
	lcdcmd_address(0x38);  									       // for using 8-bit 2 row mode and 5x7 Dots of LCD
	lcdcmd_address(0x0E);  									       // turn display ON for cursor blinking
	
	lcdcmd_address(0x01);  									       // clear screen
	lcdcmd_address(0x06);  									       // display ON
	delay(10);
			
	lcdcmd_address(0x86);  									       // bring cursor to position 6 of ROW 1	
	lcddata('H');
	lcdcmd_address(0x87);  													
	lcddata('I');
	lcddata('!');
	delay(10);
	lcdcmd_address(0xC3);								               // bring cursor to position 3 of ROW 2
	lcd_string("**GUYS**");
	delay(50);
	lcdcmd_address(0x01);  									       // clear screen
				
	lcdcmd_address(0x83);									       // bring cursor to position 3 of ROW 1
	lcd_string("WELCOME TO");
	delay(100);
	lcdcmd_address(0xC3);									       // bring cursor to position 3 of ROW 2
	lcd_string("GEMICATES");
	lcdcmd_address(0x01);  									       // clear screen
	lcdcmd_address(0x0C);									       // Display On cursor Off
		
	C1=C2=C3=1;									               // The output of all the columns will be high
	while(1)
	{
		R1=R2=R3=R4=0;									       // made all the rows zero
		if(C1==0)
			check_column_one();							       // check pressed key is a column one?
		else if(C2==0)
			check_column_two();							       // check pressed key is a column two?
		else if(C3==0)
			check_column_three();							       // check pressed key is a column three?
	}
}

void delay(unsigned int count)
{
	int m,n;
	for(m=0;m<count;m++)
	for(n=0;n<1275;n++);
}

void lcdcmd_address(unsigned char cmd)  			                                       // Function to send command to LCD
{
	Lcd_port = cmd;
	rs= 0;
	rw= 0;
	en= 1;
	delay(5);
	en= 0;
	return;
}

void lcddata(unsigned char send_data)   				                              // Function to send data to LCD
{
	Lcd_port = send_data;
	rs= 1;
	rw=0;
	en=1;
	delay(5);
	en=0;
	return;
}
 
void lcd_string(unsigned char str[10])  			                                     // Funtion to Initialize LCD
{ 
	lcd_data_string(str);
}

void lcd_data_string(unsigned char *str) 			                                     // Function to send string on LCD
{
int i=0;
	while(str[i]!='\0')
	{
		lcddata(str[i]);
		i++;
		delay(10);
	}
return;
}

void check_column_one() 									    // Detecting column one?
{
	R1=R2=R3=R4=1;										    // After detecting column All rows set to high

	R1=0;
	if((R1==0)&&(C1==0))
		output(1);
	R1=1;
	
	R2=0;
	if((R2==0)&&(C1==0))
		output(4);
	R2=1;
	
	R3=0;
	if((R3==0)&&(C1==0))
		output(7);
	R3=1;
	
	R4=0;
	if((R4==0)&&(C1==0))
		output(10);
	R4=1;
}
	
void check_column_two() 								          // Detecting column two?
{
	R1=R2=R3=R4=1;										  // After detecting column All rows set to high
	 
	R1=0;
	if((R1==0)&&(C2==0))
		output(2);
	R1=1;
	
	R2=0;
	if((R2==0)&&(C2==0))
		output(5);
	R2=1;
	
	R3=0;
	if((R3==0)&&(C2==0))
		output(8);
	R3=1;
	
	R4=0;
	if((R4==0)&&(C2==0))
		output(11);
	R4=1;
}

void check_column_three() 								        // Detecting column three?
{
	R1=R2=R3=R4=1;									        // After detecting column All rows set to high
	
	R1=0;
	if((R1==0)&&(C3==0))
		output(3);
	R1=1;
	
	R2=0;
	if((R2==0)&&(C3==0))
		output(6);
	R2=1;
	
	R3=0;
	if((R3==0)&&(C3==0))
		output(9);
	R3=1;
	
	R4=0;
	if((R4==0)&&(C3==0))
		output(12);
	R4=1;
}	

void output(int key)
{
	lcdcmd_address(0x01);								        // clear screen
	lcdcmd_address(0x86);								        // bring cursor to position 3 of ROW 1
	switch(key)
	{
		case 1:
			lcd_string("ONE");
		break;
		
		case 2:
			lcd_string("TWO");
		break;
		
		case 3:
			lcd_string("THREE");
		break;
		
		case 4:
			lcd_string("FOUR");
		break;
		
		case 5:
			lcd_string("FIVE");
		break;
		
		case 6:
			lcd_string("SIX");
		break;
		
		case 7:
			lcd_string("SEVEN");
		break;
		
		case 8:
			lcd_string("EIGHT");
		break;
		
		case 9:
			lcd_string("NINE");
		break;
		
		case 10:
			lcd_string("TEN");
		break;
		
		case 11:
			lcd_string("ELEVEN");
		break;
		
		case 12:
			lcd_string("TWELVE");
		break;
	}
}

Error message here!

Show Error message here!


Forgot your password?

Error message here!

Send OTP

Error message here!

Show Error message here!


Lost your password? Please enter your email address. You will receive a password you Need.

Send Error message here!


Back to log-in

Close